//
//  AppDelegate.m
//  SSC
//
//  Created by 红沙尘 on 17/6/6.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "AppDelegate.h"
#import "HSCDeBugButton.h"
#import "LogViewController.h"
#import "MainTabBarController.h"
#import "LoginVC.h"

#import <JHFSDK/JHFSDK.h>

/*
TODO:
 投注倍数：现在为手动点击加号翻倍数，修改为可以直接输入数字翻倍数。
 后台：加找回密码，假设客户丢失资金密码，后台可以重置帮助客户找回。
 银行卡：加解绑功能。
 充值 提现：设置声音，充值 提现声音不同。
 
DONE:
 充值问题：苹果版充值现在显示彩票名称，修改为商品名称。
 团队管理：加团队汇总。加显示客户充值金额。

 */

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.otype = CaipiaoTypeUnknow;
    self.qi = @"--";
    self.hh = @"00";
    self.mm = @"00";
    self.ss = @"00";
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    BOOL isLogin = [[Userinfo standardUserinfo] isLogin];
    
    if (!isLogin) {
        [self gotoLogin];
    } else {
        [self gotoMain];
    }

    [self checkVersion];
    
    return YES;
}

- (void)gotoMain {
    
    [DataManager standardDataManager];
    
    MainTabBarController *tabVC = [[MainTabBarController alloc] init];
    self.window.rootViewController = tabVC;
    
#if DEBUG
    // 初始化调试组件
    [self initDebug];
#endif
}

- (void)gotoLogin {

    [[Userinfo standardUserinfo] destory];
    
    self.window.rootViewController = [self navWithRootViewController:[[LoginVC alloc] init]];
    
#if DEBUG
    // 初始化调试组件
    [self initDebug];
#endif
}

- (UINavigationController *)navWithRootViewController:(UIViewController *)viweController {
    
    if (!viweController) return nil;
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viweController];
    nav.navigationBar.translucent = NO;
    nav.navigationBar.barTintColor = COLOR_FF5253;
    [nav.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    return nav;
}

//MARK: - 1.0 AppDelegate

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[JHFSDK sharedInstance] applicationWillEnterForeground:application];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ApplicationWillEnterForeground" object:nil];
//    [self checkVersion];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    NSLog(@"%@ - %@", url,url.absoluteString);
    // 支付结果处理
    [[JHFSDK sharedInstance] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDict) {
        NSLog(@"xxxxxxxxxxxxxx = %@",resultDict);
        // standbyCallback为备用回调，当调起第三方支付客户端(比如：支付宝，微信支付)在操作时，商户app进程在后台被结束，只能通过这个block输出支付结果。
        
    }];
    
    return YES;
}

//MARK: - 2.0 检测版本更新

- (void)checkVersion {
    
    [SSCRequest appUpdateWithSystem:@"1"
                            success:^(NSDictionary *responseObject) {
        NSLog(@"responseObject = %@",responseObject);
        NSDictionary *data = responseObject[@"data"];
        
        NSString *version = data[@"version"];
        
        NSString *app_Version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        
        if ([version isEqualToString:app_Version]) return ;
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"版本更新"
                                                                       message:data[@"name"]
                                                                preferredStyle:UIAlertControllerStyleAlert];

        [alert addAction:[UIAlertAction actionWithTitle:@"更新版本"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action)
                          {
                              NSURL *url = [NSURL URLWithString:data[@"url"]];
                              [[UIApplication sharedApplication] openURL:url];
                          }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        
        [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
        
    } fail:^(NSError *error) {
        
    }];
}

- (void)setOtype:(CaipiaoType)otype {
    _otype = otype;
    
    if (_otype == CaipiaoTypeUnknow) {
        self.seconds = 0;
    }
}

//MARK: - 3.0 BYDebug

#if DEBUG

- (void)initDebug
{
//    HSCDeBugButton *debugBtn = [HSCDeBugButton buttonWithType:UIButtonTypeSystem];
//    debugBtn.frame = CGRectMake(ScreenWidth - 90, ScreenHeight - 150, 50, 50);
//    debugBtn.backgroundColor = [UIColor lightGrayColor];
//    debugBtn.layer.cornerRadius = 25;
//    debugBtn.layer.masksToBounds = YES;
//    
//    [debugBtn setTitle:@"Log" forState:UIControlStateNormal];
//    [debugBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    [self.window.rootViewController.view addSubview:debugBtn];
//    
//    // 添加单击手势
//    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showLog)];
//    tapGesture.numberOfTapsRequired = 1;
//    debugBtn.userInteractionEnabled = YES;
//    [debugBtn addGestureRecognizer:tapGesture];
}

- (void)showLog {
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[LogViewController alloc] init]];
    
    [self.window.rootViewController presentViewController:nav animated:YES completion:nil];
}

#endif

@end
